from django.shortcuts import render, redirect, HttpResponse
from App01 import models


# Create your views here.

def lis_stu(request):
    all_stu = models.Student.objects.all()
    return render(request, 'lis_stu.html', {"stu": all_stu})


def add_stu(request):
    if request.method == 'POST':
        new_name = request.POST.get('s_name')
        if new_name:
            cid = request.POST.get('cid')
            models.Student.objects.create(name=new_name, class1_id=cid)
            return redirect('/lis_stu/')
        else:
            return HttpResponse('学员姓名不能为空')
    all_cls = models.Class1.objects.all()
    return render(request, 'add_stu.html', {'c_list': all_cls})


def del_stu(request):
    del_id = request.GET.get('id', None)
    if del_id:
        del_boj = models.Student.objects.get(id=del_id)
        del_boj.delete()
        return redirect('/lis_stu/')


def edit_stu(request):
    if request.method == "POST":
        new_name = request.POST.get('s_name')
        new_cls_id = request.POST.get('edit_cls')
        edit_id = request.POST.get('id')
        edit_obj = models.Student.objects.get(id=edit_id)
        edit_obj.name = new_name
        edit_obj.class1_id = new_cls_id
        edit_obj.save()
        return redirect('/lis_stu/')
    edit_id = request.GET.get('id', None)
    edit_obj = models.Student.objects.get(id=edit_id)
    all_cls = models.Class1.objects.all()
    return render(request, 'edit_stu.html', {"e_s": edit_obj, 'c_list': all_cls})


def lis_cls(request):
    all_cls = models.Class1.objects.all()
    return render(request, 'lis_cls.html', {"cls": all_cls})


def add_cls(request):
    if request.method == 'POST':
        new_name = request.POST.get('c_name')
        if new_name:
            models.Class1.objects.create(name=new_name)
            return redirect('/lis_cls/')
        else:
            return HttpResponse('班级不能为空')
    return render(request, 'add_cls.html')


def del_cls(request):
    del_id = request.GET.get('id', None)
    if del_id:
        del_boj = models.Class1.objects.get(id=del_id)
        del_boj.delete()
        return redirect('/lis_cls/')


def edit_cls(request):
    if request.method == "POST":
        new_name = request.POST.get('c_name')
        edit_id = request.POST.get('id')
        edit_obj = models.Class1.objects.get(id=edit_id)
        edit_obj.name = new_name
        edit_obj.save()
        return redirect('/lis_cls/')
    edit_id = request.GET.get('id', None)
    edit_obj = models.Class1.objects.get(id=edit_id)
    return render(request, 'edit_cls.html', {"e_c": edit_obj})


def lis_tec(request):
    all_tec = models.Teacher.objects.all()
    return render(request, 'lis_tec.html', {'tec': all_tec})


def add_tec(request):
    if request.method == 'POST':
        new_name = request.POST.get('t_name')
        if new_name:
            new_cls = request.POST.getlist("new_c")
            new_t = models.Teacher.objects.create(name=new_name)
            new_t.classes.set(new_cls)
            return redirect('/lis_tec/')
        else:
            return HttpResponse('教师姓名为空')
    all_cls = models.Class1.objects.all()
    return render(request, 'add_tec.html', {'c_list': all_cls})


def del_tec(request):
    del_id = request.GET.get('id', None)
    if del_id:
        del_boj = models.Teacher.objects.get(id=del_id)
        del_boj.delete()
        return redirect('/lis_tec/')


def edit_tec(request):
    if request.method == "POST":
        new_name = request.POST.get('t_name', None)
        new_classes = request.POST.getlist('edit_cls', None)
        edit_id = request.POST.get('id')
        edit_obj = models.Teacher.objects.get(id=edit_id)
        edit_obj.name = new_name
        edit_obj.classes = new_classes
        edit_obj.save()
        return redirect('/lis_tec/')
    edit_id = request.GET.get('id', None)
    edit_obj = models.Teacher.objects.get(id=edit_id)
    all_cls = models.Class1.objects.all()
    return render(request, 'edit_tec.html', {"e_t": edit_obj, "c_list": all_cls})
